//type 1四舍五入 2全舍 3全入 4近1

var ignore = {'uuid':'1'};
export default {
  /******************** 运算方法*/
  jia(v1, v2, xs, type) {
    var r1=0, r2=0, m, n, v1=this.toString(v1) ,v2=this.toString(v2);
    
    try {r1 = v1.split(".")[1].length} catch (e){}
    try {r2 = v2.split(".")[1].length} catch (e){} 
    n = Math.max(r1, r2);
    m = Math.pow(10, n)
    var v = ((v1 * m + v2 * m) / m);
    xs = xs ? xs :  (r1 > r2 ? r1 : r2);
    type = type ? type : 1;
    return this.xiaoshu(v, xs, type);
  },

  jian(v1, v2, xs, type) {
    var r1=0, r2=0, m, n, v1=this.toString(v1), v2=this.toString(v2);

    try {r1 = v1.split(".")[1].length} catch (e){}
    try {r2 = v2.split(".")[1].length} catch (e){} 
    n = Math.max(r1, r2);
    m = Math.pow(10, n)
    var v = ((v1 * m - v2 * m) / m);
    xs = xs ? xs :  (r1 > r2 ? r1 : r2);
    type = type ? type : 1;
    return this.xiaoshu(v, xs, type);
  },

  cheng(v1, v2, xs, type) {
    var m = 0 ,v1 = this.toString(v1) ,v2 = this.toString(v2);
    if (v1 == '0' || v2 == '0') {return '0'};

    try {m += v1.split(".")[1].length} catch (e) {}
    try {m += v2.split(".")[1].length} catch (e) {}
    var v = v1.replace(".", "") * v2.replace(".", "") / Math.pow(10, m);
    return this.xiaoshu(v, xs, type);
  },

  chu(v1, v2, xs, type) {
    var t1=0, t2=0, v1 = this.toString(v1) ,v2 = this.toString(v2);
    if (v1 == '0' || v2 == '0') {return '0'};

    try{t1 = v1.split('.')[1].length} catch(e){}
    try{t2 = v2.split('.')[1].length} catch(e){}
    var v = v1.replace(".", "") / v2.replace(".", "");
    v = this.cheng(v, Math.pow(10, t2-t1));
    return this.xiaoshu(v, xs, type);
  },


  /******************** 数据处理*/
  xiaoshu(v, xs, type=2){

    v = this.toString(v);
    if (xs==undefined) {return v};

    var r=0;
    try {r = v.split(".")[1].length} catch (e){}
    if (r <= xs) {return v}

    if (type == 1) {
      v = Number(v).toFixed(xs);
      return this.toString(v);
    }

    if (type == 2) {
      var n = r-xs;
      v = v.substring(0, v.length-n);
      return this.toString(v);
    }

    if (type == 3) {
      var n, j, c, d;
      n = r-xs;

      j = v.substring(v.length-n, v.length);
      v = v.substring(0, v.length - n);
      if (Number(j) < 5){return this.toString(v)}
      c = -(v.split(".")[1].length);
      d = Math.pow(10, c);
      v = this.jia(v, d)
      return v;
    }

    if (type == 4) {
      var n, j, c, d;
      n = r-xs;
      j = v.substring(v.length - n, v.length);
      v = v.substring(0, v.length - n);
      if (Number(j) <= 0){return this.toString(v)}
      c = -(v.split(".")[1].length);
      d = Math.pow(10, c);
      v = this.jia(v, d)
      return v;
    }
  },

  //小数去0
  xiaoShuDelete0(value){
    value = this.toString(value);
    return value;
  },

  /// 获取数-数值
  getNumber(value,){
    // jdebug.log("1")
    // jdebug.log(value)
    value = this.toString(value);
    // jdebug.log("2")
    // jdebug.log(value)
    value = value.replace('+', '');
    // jdebug.log("3")
    // jdebug.log(value)
    return Number(value);
  },

  toString(v) {

    if (v == undefined || v == ''){return '0'};
    
    if (v.constructor.name == 'Number') {
      v = v.toString();
    }

    if(v=='NaN'){return '0'};

    if (v.split("e-").length > 1) {
      var a = v.split("e-");
      v = a[0].replace('.','');
      for (let index = 0; index < (Number(a[1]) - 1); index++) {v='0'+v}
      return '0.'+v;
    }
    
    if (v.split("e+").length > 1) {
      var t1=0,t2=0,a;
      var a = v.split("e+");
      try{t1 = a[0].split('.')[0].length} catch(e){}
      try{t2 = a[0].split('.')[1].length} catch(e){}
      var n = Number(a[1]) - t2;

      v = a[0].replace('.','');
      if (n==0) {return v};
      if (n > 0) {
        for (let index = 0; index < n; index++) {v=v+'0'}
        return v;
      }
      return v.insert((t1+n), '.');
    }
    
    if(v.indexOf(".") != -1){
      v = v.replace(/(?:\.0*|(\.\d+?)0+)$/, '$1')
    }
    return v;
  },

  addJia(v){
    v = this.toString(v);
    v = (Number(v) > 0) ? ('+'+v) : v;
    return v;
  },

  add0(v, xs){
    
    v = this.toString(v);
    if (xs==undefined) {return v};

    var r=0;
    try {r = v.split(".")[1].length} catch (e){}
    if (r >= xs) {return v}

    v = Number(v).toFixed(xs);
    return this.toString(v);
  },
  
  getXiaoShuLength(value){
    value = this.toString(value);
    var location = value.indexOf(".");
    if(location == -1) {
      return 0;
    } else {
      return value.length - location - 1;
    }
  },

  /******************** 规范数据*/
  gfData(data) {

    if (data == undefined) {
      return data;
    }
    
    if (data.constructor.name == 'Number') {
      return this.toString(data);
    }

    if (data.constructor.name == 'String') {

      //字符串直接转JS不崩溃-先判断是否JS
      if (data.indexOf("{") != -1 || data.indexOf("[") != -1){
        try {
          data = JSON.parse(data);
          return this.gfData(data);
        } catch (e){}
      }

      var numRe = new RegExp(/(^[\-0-9][0-9]*(.[0-9]+)?)$/);
      if (numRe.test(data)) {
        return this.toString(data);
      } else {
        return data;
      }
    }
    
    if (data.constructor.name == 'Array') {
      for (let index = 0; index < data.length; index++) {
        var z = data[index];
        data[index] = this.gfData(z);
      }
      return data;
    }

    if (data.constructor.name == 'Object') {
      for (const key in data) {
        if (ignore[key]) continue;
        var z = data[key];
        data[key] = this.gfData(z);
      }
      return data;
    }

    return data;
  },

  gfCoin(v, coin='USDT', is0=true) {
    var xs = coin.toUpperCase()=="BTC"?6:4;
    v = this.xiaoshu(v, xs, 2);
    v = is0 ? this.add0(v, xs) : v;
    return v;
  },

  gfMoney(v, is0) {
    v = this.xiaoshu(v, 4, 2);
    v = is0 ? this.add0(v, 4) : v;
    return v;
  },

  //规划数
  guiFan(value, scale) {
    var num = this.jieQv(value, 0);
    if(num.length > 6) {
      return this.div(value, '1000000', scale, false) + 'm';
    } else if (num.length > 3) {
      return this.div(value, '1000', scale, false) + 'k';
    } else {
      return value;
    }
  },


  /******************** 其他*/
  //获取币保留位数
  getCoin(coin){
    return coin.toUpperCase()=="BTC"?6:4;
  },
}